/* Copyright (C) 2012-2018 Tal Aloni <tal.aloni.il@gmail.com>. All rights reserved.
 * 
 * You can redistribute this program and/or modify it under the terms of
 * the GNU Lesser Public License as published by the Free Software Foundation,
 * either version 3 of the License, or (at your option) any later version.
 */
using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using Utilities;

namespace ISCSI.Server
{
    internal class TransferEntry
    {
        public SCSICommandPDU Command;
        public uint NextR2TSN;
        public uint NextOffset;
        public uint TotalR2Ts; // Numbers of R2Ts that will be sent during this transfer

        public TransferEntry(SCSICommandPDU command, uint nextR2TSN, uint nextOffset, uint totalR2Ts)
        {
            Command = command;
            NextR2TSN = nextR2TSN;
            NextOffset = nextOffset;
            TotalR2Ts = totalR2Ts;
        }
    }

    internal class ConnectionParameters
    {
        public ISCSISession Session;
        public ushort CID; // connection ID, generated by the initiator
        public IPEndPoint InitiatorEndPoint;

        public int InitiatorMaxRecvDataSegmentLength = DefaultParameters.Connection.MaxRecvDataSegmentLength;
        public int TargetMaxRecvDataSegmentLength = ISCSIServer.DeclaredParameters.MaxRecvDataSegmentLength;

        public uint StatSN = 0; // Initial StatSN, any number will do
        private Dictionary<uint, string> m_textSequences = new Dictionary<uint, string>();
        private Dictionary<uint, uint> m_taskTagToTransferTag = new Dictionary<uint, uint>();
        // Dictionary of current transfers: <transfer-tag, TransferEntry>
        private Dictionary<uint, TransferEntry> m_transfers = new Dictionary<uint, TransferEntry>();

        public string AddTextToSequence(uint initiatorTaskTag, string text)
        {
            string precedingText;
            if (m_textSequences.TryGetValue(initiatorTaskTag, out precedingText))
            {
                string sequence = precedingText + text;
                m_textSequences[initiatorTaskTag] = sequence;
                return sequence;

            }
            else
            {
                m_textSequences.Add(initiatorTaskTag, text);
                return text;
            }
        }

        public void RemoveTextSequence(uint initiatorTaskTag)
        {
            m_textSequences.Remove(initiatorTaskTag);
        }

        public TransferEntry AddTransfer(uint initiatorTaskTag, uint transferTag, SCSICommandPDU command, uint nextR2TSN, uint nextOffset, uint totalR2Ts)
        {
            m_taskTagToTransferTag.Add(initiatorTaskTag, transferTag);
            TransferEntry entry = new TransferEntry(command, nextR2TSN, nextOffset, totalR2Ts);
            m_transfers.Add(transferTag, entry);
            return entry;
        }

        public TransferEntry GetTransferEntryUsingTaskTag(uint initiatorTaskTag)
        {
            uint transferTag;
            if (m_taskTagToTransferTag.TryGetValue(initiatorTaskTag, out transferTag))
            {
                return GetTransferEntry(transferTag);
            }
            return null;
        }

        public TransferEntry GetTransferEntry(uint transferTag)
        {
            TransferEntry result;
            m_transfers.TryGetValue(transferTag, out result);
            return result;
        }

        public void RemoveTransfer(uint initiatorTaskTag, uint transferTag)
        {
            m_taskTagToTransferTag.Remove(initiatorTaskTag);
            m_transfers.Remove(transferTag);
        }

        public string ConnectionIdentifier
        {
            get
            {
                if (Session != null)
                {
                    return Session.SessionIdentifier + String.Format(",CID={0}", this.CID.ToString("x"));
                }
                else
                {
                    return String.Format("ISID=0,TSIH=0,CID={0}", this.CID.ToString("x"));
                }
            }
        }
    }
}
